package com.itheima.mobili;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConstant;
import com.itheima.common.utils.RedisUtils;
import com.itheima.pojo.Member;
import com.itheima.pojo.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @version V1.0
 * @author: yongjichu
 * @date: 2020/1/16 19:53
 * @description:
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference(cluster = "failfast")
    private SetmealService setmealService;
    @Autowired
    RedisUtils redisUtils;

    /**
     * 查询所有的套餐信息
     * @return
     */
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(HttpServletRequest request){
       /* //获取前端传过来的token，这个token是为了验证用户是否登陆
        String token = request.getHeader("token");
        if(null==token){
            //说明没有登陆
            return Result.error(false,"请登录");
        }
        String jsonMember = redisUtils.get(token);
        //将字符串转为pojo对象
        Member member = JSON.parseObject(jsonMember, Member.class);
        if(null==member){
            return Result.error(false,"请登录");
        }*/
       //将以上注释的代码使用springMvc代替
        try {
            List<Setmeal> setmealList=setmealService.getSetmeal();
            return Result.success(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 根据套餐的id获取套餐的信息，套餐包含的检查组的信息，还有检查组包含的检查项的信息
     * 然后将这些个信息进行回显到页面
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal=setmealService.findByIdBatch(id);
            return Result.success(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/findDetilById")
   public Result findDetilById(Integer id){
        try {
            Setmeal setmeal=setmealService.findDetilById(id);
            return Result.success(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
   }
    @RequestMapping("/getToken")
    public Result getToken(){
        //生成一个token
        String token = UUID.randomUUID().toString();
        //将token存入redis中
        redisUtils.setex(token,token,60*5);
        return Result.success(true,"",token);
    }

    @RequestMapping("cs")
    public void ceshi(@com.itheima.ArgumentResolver.Member Member member){
        Integer id = member.getId();
        System.out.println(member);
    }
}
